<template>
	<view class="content">
		<scroll-view scroll-y class="left-aside">
			<view v-for="item in flist" :key="item.id" class="f-item b-b" :class="{active: item.id == currentId}" @click="tabtap(item)">
				{{item.text}}
			</view>
		</scroll-view>
		<scroll-view scroll-with-animation scroll-y class="right-aside"  @scrolltolower="srollbottom">
			
			<view class="t-list">
				<view  class="t-item" v-for="titem in slist" :key="titem.id">
					<image @click="navToList(titem)" class="imgwidth" style="border-radius: 8rpx;" :src="titem.image"></image>
					<view class="title-wrap">{{titem.title}}</view>
					<view class="wenzi-wrap">
						
						<view class="price-wrap">
							￥{{titem.price}}
						</view>
						<view class="num-wrap" @tap="addCart(titem)">
							<!-- 销量:{{titem.sales}} -->
						</view>
					</view>
				</view>
			</view>
			<uni-load-more  :status="more">
			</uni-load-more>
		</scroll-view>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				more:'loading',
				nowpage:1,
				sizeCalcState: false,
				
				currentId: 1,
				flist: [],
				slist: [],
				tlist: [],
				fenlei:''
			}
		},
		
		onReachBottom() {
			if(this.more=='nomore'){
				return
			}
			console.log('?????????????')
			this.loadData();
		},
		onShow(options){
			this.fenlei=uni.getStorageSync('cate_id').split('=')[1];
			
			this.loadData();
		},
		onHide(){
			uni.removeStorageSync('cate_id');
		},
		onPullDownRefresh(){
			this.more='more'
			this.slist=[]
			this.nowpage=1
		    this.loadData();
		},
		methods: {
			srollbottom(){
				if(this.more=='nomore'){
					return
				}
				
				this.getListData();
			},
			addCart(item){
				
			
				let that=this
				let data={
					id:item.id,
					number:1
				}
				
			
				
				that.apipost('/api/home/add_cart',data,true).then(res => {
					if(res.Code==10200){
						uni.showToast({
						    title: res.Message,
						    duration:4000,
							icon:'none'
						});
						
					}
					
				}).catch(err=>{
					console.log(err)
				});
				
				
			},
			navToDetailPage(item) {
				
				let id = item.id;
				uni.navigateTo({
					url: `/pages/productdetail/index?id=${id}`
				})
			},
			loadData(){
				let that=this
					that.apipost('/api/category').then(res => {
						if(res.Code==10200){
							that.flist=res.Data
							
							
							if(that.fenlei){
								that.currentId=that.fenlei
							}else{
								that.currentId=res.Data?res.Data[0].id:''
							}
							
							
							that.getListData()
							uni.stopPullDownRefresh()
						}
						
					}).catch(err=>{
						uni.stopPullDownRefresh()
						console.log(err)
					});
			},
			getListData(){
				let that=this
					that.apipost('/api/category/cate_products',{
						id:that.currentId,
						page:that.nowpage
					},true).then(res => {
						if(res.Code==10200){
							
							let data=res.Data
							that.slist=that.slist.concat(data)
							++that.nowpage
							uni.stopPullDownRefresh()
							if(data.length<15){
								that.more='nomore'
							}
							
							
							
						}
						
					}).catch(err=>{
						console.log(err)
					});
			},
			//一级分类点击
			tabtap(item){
				// if(!this.sizeCalcState){
				// 	this.calcSize();
				// }
				this.currentId = item.id;
				this.more='more'
				this.slist=[]
				this.nowpage=1
				this.getListData()
				
				// let index = this.slist.findIndex(sitem=>sitem.pid === item.id);
				// this.tabScrollTop = this.slist[index].top;
			},

			//计算右侧栏每个tab的高度等信息
			calcSize(){
				let h = 0;
				this.slist.forEach(item=>{
					let view = uni.createSelectorQuery().select("#main-" + item.id);
					view.fields({
						size: true
					}, data => {
						item.top = h;
						h += data.height;
						item.bottom = h;
					}).exec();
				})
				this.sizeCalcState = true;
			},
			navToList(item){
				let id = item.id;
				uni.navigateTo({
					url: `/pages/productdetail/index?id=${id}`
				})
			}
		}
	}
</script>

<style lang='scss'>
	.price-wrap{
		
		color:#fa436a;
		font-size:35rpx;
	}
	.wenzi-wrap{
		padding-top:6rpx;
		align-items: center;
		width:100%;
		display:flex;
		justify-content: space-between;
	}
	.num-wrap{
		width:50rpx;
		height:50rpx;
		background: url(/static/xinzengpng.png) no-repeat center;
		background-size:100% 100%;
		color:#848689;
	}
	.title-wrap{
		font-size:32rpx;
		padding-top:6rpx;
		width: 100%;
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
	}
	page,
	.content {
		height: 100%;
		background-color: #f8f8f8;
	}

	.content {
		display: flex;
	}
	.left-aside {
		flex-shrink: 0;
		width: 180rpx;
		height: 100%;
		background-color: #fff;
	}
	.f-item {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 100%;
		height: 100rpx;
		font-size: 28rpx;
		color: $font-color-base;
		position: relative;
		&.active{
			color: $base-color;
			background: #f8f8f8;
			&:before{
				content: '';
				position: absolute;
				left: 0;
				top: 50%;
				transform: translateY(-50%);
				height: 36rpx;
				width: 8rpx;
				background-color: $base-color;
				border-radius: 0 4px 4px 0;
				opacity: .8;
			}
		}
	}

	.right-aside{
		flex: 1;
		/* overflow: hidden; */
		padding-left: 6rpx;
	}
	.s-item{
		display: flex;
		align-items: center;
		height: 70rpx;
		padding-top: 8rpx;
		font-size: 28rpx;
		color: $font-color-dark;
	}
	.t-list{
		padding:0 10rpx;
		box-sizing: border-box;
		display: flex;
		flex-wrap: wrap;
		width: 100%;
		background: #fff;
		justify-content: space-between;
		padding-top: 12rpx;
		/* &:after{
			content: '';
			flex: 99;
			height: 0;
		} */
	}
	.t-item{
		flex-shrink: 0;
		display: flex;
		justify-content: center;
		align-items: center;
		flex-direction: column;
		width:260rpx;
		font-size: 26rpx;
		color: #666;
		padding-bottom: 20rpx;
		
		.imgwidth{
			width: 260rpx;
			height: 260rpx;
		}
	}
</style>
